﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.GiaoVienService;
namespace eTMS.Controllers{
public class GiaoVienControllerBase : CommonController{
	public GiaoVienControllerBase()
		: base()
	{	
		this.dao = new GiaoVienServiceClient();
	}
	~GiaoVienControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private GiaoVienEntity oGiaoVien;	
	public GiaoVienEntity GiaoVienObject{
		get{
			return oGiaoVien;
		}
		set{
			oGiaoVien=value;
		}
	}
	protected GiaoVienServiceClient dao;
	public DataTable Table{
		get{
			return _Table;
		}
        set
        {
            _Table = value;
        }
	}
	public void LoadAllDSGiaoVien(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oGiaoVien==null)return false;
		bool result=true;
		try{
			oGiaoVien = dao.SaveRet(oGiaoVien);
		}
		catch{
			result=false;
		}
		if(result){
			_ListGiaoVien.Add(oGiaoVien);
		}
		return result;
	}
	public bool Update(){
		if(oGiaoVien==null)return false;
		return dao.Update(oGiaoVien);
	}
	public bool Delete(){
		if(oGiaoVien==null)return false;
		bool retvalue = dao.Delete(oGiaoVien);
		if(retvalue){			
			_ListGiaoVien.Remove(oGiaoVien);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 GiaoVien_ID){
		bool retvalue = dao.DeleteByID(GiaoVien_ID);		
		if(retvalue){
			GiaoVienEntity oDeleted=FindGiaoVien(GiaoVien_ID);
			if(oDeleted!=null){
				_ListGiaoVien.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oGiaoVien = new GiaoVienEntity();
	}
	//Check if exists MaGiaoVien
	public bool CheckExistsMaGiaoVien(object value){	
		return dao.SelectOneByMaGiaoVien(value.ToString()).MaGiaoVien!=null;
    }    
    //Copy data from data row in grid to object
    public GiaoVienEntity MapDataRowToObject(DataRow row){
		GiaoVienEntity o = new GiaoVienEntity();
		 o.MaGiaoVien=Convert.ToString(row["MaGiaoVien"]);
		 o.HoTenGiaoVien=Convert.ToString(row["HoTenGiaoVien"]);
		 o.NgaySinh=Convert.ToString(row["NgaySinh"]);
         o.DiaChi = Convert.ToString(row["DiaChi"]);
         o.DienThoai = Convert.ToString(row["DienThoai"]);
         o.Email = Convert.ToString(row["Email"]);
		 if(!row.IsNull("BoMon_ID"))
			o.BoMon_ID=ConvertInt(row["BoMon_ID"]);
		 if(!row.IsNull("GiaoVien_ID"))
             o.GiaoVien_ID = ConvertInt(row["GiaoVien_ID"]); 
        if (!row.IsNull("GioiTinh"))
            o.GioiTinh =ConvertByte(row["GioiTinh"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(GiaoVienEntity o,DataRow row){
		row["MaGiaoVien"]=o.MaGiaoVien;
		row["HoTenGiaoVien"]=o.HoTenGiaoVien;
		row["NgaySinh"]=o.NgaySinh;
        row["DiaChi"] = o.DiaChi;
        row["Email"] = o.Email;
        row["GioiTinh"] = o.GioiTinh;
        row["DienThoai"] = o.DienThoai;
		row["BoMon_ID"]=o.BoMon_ID;
		row["GiaoVien_ID"]=o.GiaoVien_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.GiaoVienObject,row);
    }
	public void LoadObject(Int32 GiaoVien_ID){
		GiaoVienEntity oLoad=FindGiaoVien(GiaoVien_ID);
		if(oLoad==null){
			oGiaoVien=this.LoadGiaoVien(GiaoVien_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oGiaoVien.IsNullGiaoVien_ID;
			if(!bNotIsNull){
				this.ListGiaoVien.Add(oGiaoVien);
			}
		}
		else{
			oGiaoVien=oLoad;
		}
	}
	#region CachedList GiaoVien
	private static List<GiaoVienEntity> _ListGiaoVien=new List<GiaoVienEntity>();
    public List<GiaoVienEntity>ListGiaoVien{
		get{
			return _ListGiaoVien;
		}
	}
	public static List<GiaoVienEntity>ListGiaoVienStatic{
		get{
			return _ListGiaoVien;
		}
	}
	public GiaoVienEntity FindGiaoVien(Int32 GiaoVien_ID){
		GiaoVienEntity oResult=this.ListGiaoVien.Find(delegate(GiaoVienEntity oFind){
			bool result=true; 
			result&=oFind.GiaoVien_ID.Equals(GiaoVien_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllGiaoVien(bool clearCache){
		if(_ListGiaoVien.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		GiaoVienServiceClient dao = new GiaoVienServiceClient();
		_ListGiaoVien=new List<GiaoVienEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public GiaoVienEntity LoadGiaoVien(Int32 GiaoVien_ID){			
		GiaoVienEntity o=dao.SelectOne(GiaoVien_ID);		
		return o;
	}
	#endregion
}
}
